package com.zfy.cmsservice.controller;


import com.alibaba.excel.EasyExcel;
import com.zfy.cmsservice.entity.ExamScores;
import com.zfy.cmsservice.entity.Students;
import com.zfy.cmsservice.entity.excel.ExamScoreDTO;
import com.zfy.cmsservice.entity.result.ScoreStatistic;
import com.zfy.cmsservice.service.ExamScoresService;
import com.zfy.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 考试成绩表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-12-10
 */
@RestController
@Api(description = "考试成绩管理-成绩信息维护")
@RequestMapping("/cmsservice/exam-scores")
public class ExamScoresController {
    @Autowired
    ExamScoresService examScoresService;

    // 分页查询考试信息 根据分数检索
    @ApiOperation(value = "分页查询考试成绩信息")
    @PostMapping("pageExamScores/{page}/{size}")
    public R pageExamScores(@PathVariable long page, @PathVariable long size,
                            @RequestParam(required = false) Integer begin,
                            @RequestParam(required = false) Integer end) {
        return examScoresService.pageExamScores(page,size,begin,end);
    }

//    添加成绩
    @ApiOperation(value = "添加成绩")
    @PostMapping("addExamScores")
    public R addExamScores(@RequestBody ExamScores examScores) {
        boolean flag = examScoresService.save(examScores);
        if(flag) {
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

//    修改成绩
    @ApiOperation(value = "修改成绩")
    @PostMapping("updateExamScores")
    public R updateExamScores(@RequestBody ExamScores examScores) {
        if (StringUtils.isEmpty(examScores)) {
            // 传参错误异常
            return R.httpError();
        }
        boolean flag = examScoresService.updateById(examScores);
        if(flag) {
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    // 逻辑删除学生信息
    @ApiOperation(value = "逻辑删除学生信息")
    @PostMapping("removeExamScores/{id}")
    public R removeExamScores(@PathVariable String id){
        boolean remove = examScoresService.removeById(id);
        if(remove) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    //    根据id查询成绩
    @ApiOperation(value = "根据id查询成绩")
    @GetMapping("getExamScores/{id}")
    public R getExamScoresById(@PathVariable String id) {
        ExamScores byId = examScoresService.getById(id);
        return R.ok().data("item", byId);
    }

//    根据身份证信息查询成绩
    @ApiOperation(value = "根据身份证信息查询成绩")
    @GetMapping("getExamScores")
    public R getExamScores(@RequestParam String idNumber,
                           @RequestParam(required = false) String semesterId) {
        return examScoresService.getExamScores(idNumber,semesterId);
    }

    @ApiOperation(value = "成绩统计")
    @GetMapping("scoreStatistics")
    public R getScoreStatistics() {
        List<ScoreStatistic> scoreStatistics = examScoresService.getScoreStatistics();
        List<String> courseNames = new ArrayList<>();
        List<Integer> totalStudents = new ArrayList<>();
        List<Double> averageScores = new ArrayList<>();

        for (ScoreStatistic statistic : scoreStatistics) {
            courseNames.add(statistic.getCourseName());
            totalStudents.add(statistic.getTotalStudents());
            averageScores.add(statistic.getAverageScore());
        }

        Map<String, List<?>> result = new HashMap<>();
        result.put("courseNames", courseNames);
        result.put("totalStudents", totalStudents);
        result.put("averageScores", averageScores);

        return R.ok().data("result",result);
    }

    @GetMapping("/exportScores")
    public void exportExamScores(@RequestParam(required = false) String semestersId,
                                 @RequestParam String classesId, HttpServletResponse response) throws IOException {
        List<ExamScoreDTO> data = examScoresService.getExamScoreStatistics(semestersId, classesId);
        //  解决导出为zip文件的问题
        response.setContentType("application/vnd.ms-excel");
        EasyExcel.write(response.getOutputStream(), ExamScoreDTO.class).sheet("Sheet1").doWrite(data);
    }
}

